package util;

import java.io.File;

import weka.core.Instances;
import weka.core.converters.ArffLoader;
import weka.core.converters.C45Loader;
import weka.core.converters.CSVLoader;

/**
 * Data Manager: Getting data from different format
 * @author tigerzhong
 *
 */
public class DataManager {
    
    /**
     * Get Data from file in CSV data format.
     * @param name
     * @return Instances
     * @throws Exception
     */
    public static Instances getCSVDataFromReader(String name) throws Exception {
	CSVLoader loader = new CSVLoader();
	loader.setFile(new File(name));
	loader.setSource(new File(name));
	Instances data = loader.getStructure();
	data.setClassIndex(data.numAttributes()-1);
	return data;
    }
    
    /**
     * Get Data from file in C4.5 data format. !!Warning: Need .data!!
     * @param name
     * @return Instances
     * @throws Exception
     */
    public static Instances getC45DataFromReader(String name) throws Exception {
	C45Loader loader = new C45Loader();
	loader.setFile(new File(name));
	loader.setSource(new File(name));
	Instances data = loader.getStructure();
	data.setClassIndex(data.numAttributes()-1);
	return data;
    }
    
    /**
     * Get Data from file in Arff data format.
     * @param name
     * @return Instances
     * @throws Exception
     */
    public static Instances getArffDataFromReader(String name) throws Exception {
	ArffLoader loader = new ArffLoader();
	loader.setFile(new File(name));
	loader.setSource(new File(name));
	Instances data = loader.getStructure();
	data.setClassIndex(data.numAttributes()-1);
	return data;
    }
}
